<?php
    include_once("checkAuthenticationAction.php");
    include_once("../service/categoryService.php");
    $cat_id = (isset($_POST["edit_cate_id"])?$_POST["edit_cate_id"]:0);
    $title = $_POST["categoryTitle"];
    $parent = (isset($_POST["parent_category"]) ? $_POST["parent_category"] : 0);
    $order = ($_POST["categoryOrder"] == "" ? 0 : $_POST["categoryOrder"]);
    $attributes = isset($_POST["selectedAtts"])?$_POST["selectedAtts"]:array();
	$options = isset($_POST["selectedOpts"])?$_POST["selectedOpts"]:array();
    //echo "cat_id:".$cat_id;
    if (strlen($parent) == 0){
        $parent = 0;
    }
    updateCategory($cat_id,$title,$parent,$order);
    
    unassginAllAttributeOfCategory($cat_id);
    unassginAllOptionOfCategory($cat_id);
    
    //clear values    
        for($i = 0; $i < count($attributes); $i++){
            //echo "is int:".is_numeric($attributes[$i])."<br/>";
            //echo $attributes[$i]."<br/>";
            
            if(is_numeric($attributes[$i])){
                assignAttToCate($cat_id,$attributes[$i]); 
            }else{
                createAttribute($attributes[$i]);
                $newAttId = mysql_insert_id();
                assignAttToCate($cat_id,$newAttId);             
            }
        }
	//clear options
		for($i = 0; $i < count($options); $i++){
            //echo "is int:".is_numeric($options[$i])."<br/>";
            //echo $options[$i]."<br/>";
            
            if(is_numeric($options[$i])){
                assignOptToCate($cat_id,$options[$i]); 
            }else{
                createOption($options[$i]);
                $newOptId = mysql_insert_id();
                assignOptToCate($cat_id,$newOptId);             
            }
        }
		echo "<script>window.location.href='../admin/category-detail.php?action=9&id=".$cat_id."';</script>";	
?>
